<?php

namespace app\index\controller;

use app\index\model\User as ModelUser;
use frame\lib\Db;
use frame\lib\VerfiyParam;

class User extends Base
{
    public function index()
    {
        $this->view('index');
    }
    public function list()
    {
        $query = [
            'username' => @$_GET['username'],
            'imei' => @$_GET['imei']
        ];
        success(ModelUser::list($query));
    }
    public function edit()
    {
        $data = Db::name('user')->where(['id' => $_GET['id'], 'user_id' => $this->uid])->find();
        $this->view('edit', ['data' => $data]);
    }
    public function doEdit()
    {
        $param = $_POST;
        $uid = @$param['id'];
        $data = [];
        $rule = [
            'vip_time|会员时长' => 'int',
            'score|积分' => 'int',
            'email|邮箱' => 'email',
            'status|邮箱' => 'in:1,0,2',
            'type_s|会员操作类型' => 'in:1,2',
            'id' => 'require|int',
            'password|密码' => 'min:6|max:12|alphaNum'
        ];
        VerfiyParam::init($param, $rule);
        $user = Db::name('user')->where(['id' => $uid, 'role' => 2, 'user_id' => $this->uid])->find();
        if (!$user) error('用户不存在');
        $user_vip_time = getTime(3, $user['vip_time']) < time() ? time() :  getTime(3, $user['vip_time']);
        if (@$param['type_s'] == 1) {
            $vip_time = $user_vip_time + @$param['vip_time'];
        } else {
            $vip_time = $user_vip_time - @$param['vip_time'];
            $vip_time = $vip_time < 0 ? 0 : $vip_time;
        }
        $data['password'] = @$param['password'] ? encrypt_pwd($param['password']) : $user['password'];
        $data['email'] = @$param['email'] ?? $user['email'];
        $data['score'] = @$param['score']  ?? $user['score'];
        $data['vip_time'] = getTime(2, $vip_time);
        $data['status'] = @$param['status']  ?? $user['status'];
        $data['update_time'] = getTime();
        $res = Db::name('user')->where(['id' => $uid])->update($data);
        if ($res) success();
        error();
    }
    public function del()
    {
        $id = @$_GET['id'];
        if (!$id) error('缺少参数ID', 201);
        $id = explode(',', $_GET['id']);
        if (in_array($this->uid, $id)) error('不能删除自己哦！');
        $res = Db::name('user')->where(['user_id'  => $this->uid])->whereIn('id', $id)->delete();
        if ($res) success();
        error();
    }
    public function add()
    {
        $this->view('add');
    }
    public function doAdd()
    {
        $param = $_POST;
        $rule = [
            'username|账号' => 'require|min:6|max:12|alphaNum',
            'password|密码' => 'require|min:6|max:12|alphaNum',
            'email|邮箱' => 'require|email',
            'score|积分' => 'int',
            'status|用户状态' => 'in:0,1,2',
            'vip_time|会员时长' => 'int'
        ];
        VerfiyParam::init($param, $rule);
        $hasUser = Db::name('user')->where(['username' => $param['username'], 'user_id' => $this->uid])->find();
        if ($hasUser) error('已存在该用户名，请更换一个吧 !');
        $data =  [
            'password' => encrypt_pwd($param['password']),
            'username' => $param['username'],
            'status' => $param['status'],
            'score' => $param['score'],
            'email' => $param['email'],
            'user_id' => $this->uid,
            'role' => 2,
            'create_time' => getTime()
        ];
        $data['vip_time'] = !empty(@$param['vip_time']) ? getTime(2, @$param['vip_time'] + time()) : '';
        $res = Db::name('user')->insert($data);
        if ($res) success();
        error();
    }
}
